package com.wy.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wy.common.result.Result;
import com.wy.model.entity.Teacher;
import com.wy.model.vo.TeacherQueryVo;
import com.wy.system.exception.MyException;
import com.wy.system.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/system/teacher")
public class TeacherController {
  @Autowired
  private TeacherService teacherService;

  //查询所有Teacher
  @GetMapping("findAll")
  public Result findAllTeacher(){
    List<Teacher> list = teacherService.list();
    return Result.ok(list);
  }

  //删除Teacher
  @DeleteMapping("remove/{id}")
  public Result removeTeacher(@PathVariable("id") Integer teaid){
    boolean isSuccess = teacherService.removeById(teaid);
    if (isSuccess){
      return Result.ok();
    }else {
      return Result.fail();
    }
  }

  //批量删除接口
  @DeleteMapping("batchRemove")
  public Result batchRemove(@RequestBody List<Integer> ids){
    boolean isSuccess = teacherService.removeByIds(ids);
    if (isSuccess){
      return Result.ok();
    }else {
      return Result.fail();
    }
  }

  //分页查询Teacher;page表当前页  limit每页记录数
  @GetMapping("{page}/{limit}")
  public Result findQueryTeacher(@PathVariable Integer page,
                                 @PathVariable Integer limit,
                                 TeacherQueryVo teacherQueryvo){
    //创建Page对象
    Page<Teacher> pageParam = new Page<>(page, limit);
    //调用service方法
    IPage<Teacher> pageModel = teacherService.selectPage(pageParam,teacherQueryvo);
    //返回
    return Result.ok(pageModel);
  }

  //添加Teacher对象
  //@RequestBody 不能使用get提交方式
  //传递json个是数据，把json格式数据封装到对象里面
  @PostMapping("save")
  public Result saveTeacher(@RequestBody Teacher teacher){
    boolean isSuccess = teacherService.save(teacher);
    if (isSuccess){
      return Result.ok();
    }else {
      return Result.fail();
    }
  }

  //修改 根据id查询
  @GetMapping("findTeacherById/{id}")
  public Result findTeacherById(@PathVariable("id") Integer id){
    Teacher teacher = teacherService.getById(id);
    return Result.ok(teacher);
  }

  //修改 更新数据库
  @PostMapping("update")
  public Result updateTeacher(@RequestBody Teacher teacher){
    boolean isSuccess = teacherService.updateById(teacher);
    if (isSuccess){
      return Result.ok();
    }else {
      return Result.fail();
    }
  }
}
